package com.edu.dreams.controller;

import com.edu.dreams.entity.Anim;
import com.edu.dreams.entity.User;
import com.edu.dreams.mapper.AnimMapper;
import com.edu.dreams.mapper.NewsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

//@Controller注解
@Controller

public class AnimController {

	@Autowired
	AnimMapper animMapper;

	@Autowired
	NewsMapper newsMapper;

	@RequestMapping("animations")
	public String animations(Model model) {
		model.addAttribute("animdatas", animMapper.getAllAnim());
		return "animations";
	}

	@PostMapping("addanimation")
	@ResponseBody
	public String addanimation(HttpServletRequest request, Anim anim, @RequestParam("pic") MultipartFile pic,
			@RequestParam("shipin") MultipartFile shipin, @RequestParam("pic1") MultipartFile pic1,
			@RequestParam("pic2") MultipartFile pic2, @RequestParam("pic3") MultipartFile pic3) {
		int res = 0;
		try {
			if (!(pic.isEmpty() && shipin.isEmpty() && pic1.isEmpty() && pic2.isEmpty() && pic3.isEmpty())) {
				anim.setAnim_picurl(doSaveFile(request, pic));
				anim.setAnim_vedio_url(doSaveFile(request, shipin));
				anim.setAnim_j1_picurl(doSaveFile(request, pic1));
				anim.setAnim_j2_picurl(doSaveFile(request, pic2));
				anim.setAnim_j3_picurl(doSaveFile(request, pic3));
				res = animMapper.addAnim(anim);
			} else {
				return "<script>alert('添加失败!文件上传失败！');window.location.href='seeanimation';</script>";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "<script>alert('添加失败!');window.location.href='seeanimation';</script>";
		}
		return "<script>alert('" + ((res == 1) ? "添加成功！" : "添加失败！")
				+ "');window.location.href='seeanimation';</script>";
	}

	static String doSaveFile(HttpServletRequest request, MultipartFile file) throws IOException {
		String basepath = request.getServletContext().getRealPath("/upload/");
		File filePath = new File(basepath);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		String newFileName = UUID.randomUUID() + "_" + file.getOriginalFilename();
		file.transferTo(new File(basepath + newFileName));
		return newFileName;
	}

	// 向添加动画页面跳转
	@RequestMapping("addanimpagepage")
	public String addAnimpagePage() {
		return "addanimation";
	}

	@RequestMapping("seeanimation")
	public String seenew(@RequestParam(required = false, defaultValue = "1") int startPage,
			@RequestParam(required = false, defaultValue = "6") int pagesize, Model model) {
		PageHelper.startPage(startPage, pagesize);
		PageInfo<Anim> animData = new PageInfo<>(animMapper.getAllAnim());
		model.addAttribute("pageNumNow", startPage);
		model.addAttribute("anims", animData);
		return "seeanimation";
	}

	@RequestMapping("updateAnimationPage")
	public String updateNewsPage(int id, Model model) {
		model.addAttribute("info", animMapper.getAnimById(id));
		return "update_animation";
	}

	@PostMapping("updateAnimation")
	@ResponseBody
	public String updateAnimation(HttpServletRequest request, Anim anim, @RequestParam("pic") MultipartFile pic,
			@RequestParam("shipin") MultipartFile shipin, @RequestParam("pic1") MultipartFile pic1,
			@RequestParam("pic2") MultipartFile pic2, @RequestParam("pic3") MultipartFile pic3) {
		int res = 0;
		try {
			Anim anim1 = animMapper.getAnimById(anim.getAnim_id());
			if (!(pic.isEmpty() && shipin.isEmpty() && pic1.isEmpty() && pic2.isEmpty() && pic3.isEmpty())) {
				if (!anim1.getAnim_picurl().contains(pic.getOriginalFilename())) {
					anim.setAnim_picurl(doSaveFile(request, pic));
				} else {
					anim.setAnim_picurl(anim1.getAnim_picurl());
				}
				if (!anim1.getAnim_vedio_url().contains(shipin.getOriginalFilename())) {
					anim.setAnim_vedio_url(doSaveFile(request, shipin));
				} else {
					anim.setAnim_vedio_url(anim1.getAnim_vedio_url());
				}
				if (!anim1.getAnim_j1_picurl().contains(pic1.getOriginalFilename())) {
					anim.setAnim_j1_picurl(doSaveFile(request, pic1));
				} else {
					anim.setAnim_j1_picurl(anim1.getAnim_j1_picurl());
				}
				if (!anim1.getAnim_j2_picurl().contains(pic2.getOriginalFilename())) {
					anim.setAnim_j2_picurl(doSaveFile(request, pic2));
				} else {
					anim.setAnim_j2_picurl(anim1.getAnim_j2_picurl());
				}
				if (!anim1.getAnim_j3_picurl().contains(pic3.getOriginalFilename())) {
					anim.setAnim_j3_picurl(doSaveFile(request, pic3));
				} else {
					anim.setAnim_j3_picurl(anim1.getAnim_j3_picurl());
				}
				res = animMapper.updateAnim(anim);
			} else {
				return "<script>alert('更新失败!文件上传失败！');window.location.href='seeanimation';</script>";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "<script>alert('更新失败!');window.location.href='seeanimation';</script>";
		}
		return "<script>alert('" + ((res == 1) ? "更新成功！" : "更新失败！")
				+ "');window.location.href='seeanimation';</script>";
	}

	@RequestMapping("deleteanim")
	@ResponseBody
	public String deleteAnimation(int id) {
		int res = animMapper.deleteAnim(id);
		return "<script>alert('" + ((res == 1) ? "删除成功！" : "删除失败！")
				+ "');window.location.href='seeanimation';</script>";
	}

	@RequestMapping("animationshow")
	public String animationshow(HttpServletRequest request, Integer id, Model model) {
		if (id == null) {
			return "redirect:animations";
		}
		animMapper.addLiShi(((User) request.getSession().getAttribute("user")).getUser_id(),
				animMapper.getAnimById(id).getAnim_name());
		animMapper.addViewConutById(id);
		Anim anim = animMapper.getAnimById(id);
		model.addAttribute("animdata", anim);
		model.addAttribute("animdatas", animMapper.getAllAnim());
		model.addAttribute("newsdata", newsMapper.getAllNews());
		return "animationsshow";
	}

	@PostMapping("searchAnim")
	@ResponseBody
	public String searchAnim(HttpServletRequest request, String animname) {
		if (StringUtils.isEmpty(animname)) {
			return "<script>alert('搜索内容不能为空！');window.location.href='home';</script>";
		} else {
			return "<script>window.location.href='" + request.getContextPath() + "/search?aname=" + animname
					+ "&type=1';</script>";
		}
	}

	@RequestMapping("addShouCang")
	@ResponseBody
	public String addShouCang(HttpServletRequest request, String aname) {
		int userid = ((User) request.getSession().getAttribute("user")).getUser_id();
		int res = animMapper.addShouCang(userid, aname);
		return "<script>alert('" + ((res == 1) ? "添加成功！" : "添加失败！") + "');window.location.href='"
				+ request.getContextPath() + "/home';</script>";
	}

	@RequestMapping("deleteShouCang")
	@ResponseBody
	public String deleteShouCang(HttpServletRequest request, Integer id) {
		int res = animMapper.deleteShouCang(id);
		return "<script>alert('" + ((res == 1) ? "删除成功！" : "删除失败！") + "');window.location.href='"
				+ request.getContextPath() + "/personal';</script>";
	}

	@RequestMapping("deleteLiShi")
	@ResponseBody
	public String deleteLiShi(HttpServletRequest request, Integer id) {
		int res = animMapper.deleteLiShi(id);
		return "<script>alert('" + ((res == 1) ? "删除成功！" : "删除失败！") + "');window.location.href='"
				+ request.getContextPath() + "/personal';</script>";
	}
}
